﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using VehicleManagement.Models;
using VehicleManagement.ControllersServices;
using VehicleManagement.Codes;
using System.Data.Linq;
using System.Web.Script.Serialization;

namespace VehicleManagement.WebService
{
    /// <summary>
    /// WebService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    //[SoapRpcService]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [SoapDocumentService(RoutingStyle =SoapServiceRoutingStyle.RequestElement)]
    //[System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="driverLoginName">登录名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        [WebMethod]
        public bool Login(string driverLoginName,string password)
        {
            DriverServices ds = new DriverServices();
            return ds.DriverLogin(driverLoginName, password);
        }

        //[WebMethod]
        //public Track GetTrack(string driverLoginName)
        //{
        //    DriverServices ds = new DriverServices();
        //    int id = ds.GetDriverIdByLoginName(driverLoginName);
        //    Table<Track> tracks = VehicleManagementDataContextManager.Instance.GetTable<Track>();
        //    Track track = tracks.Single(t => t.DriverID == id && t.State == 0);
        //    return track;
        //}

        /// <summary>
        /// 获取路线
        /// </summary>
        /// <param name="driverLoginName"></param>
        /// <returns></returns>
        [WebMethod]
        public string GetTrack(string driverNumber)
        {
            //DriverServices ds = new DriverServices();
            //int id = ds.GetDriverIdByLoginName(driverLoginName);
            Table<Track> tracks = VehicleManagementDataContextManager.Instance.GetTable<Track>();
            var track = from t in tracks 
                        where t.DriverNumber == driverNumber && t.State == 0 
                        
                        select t;
            JavaScriptSerializer jss = new JavaScriptSerializer();
           
            return jss.Serialize(track);
        }
        [WebMethod]
        public string UpdateApp()
        {
            return "2,VehicleManagement,http://shouji.baidu.com/download/1426l/AppSearch_Android_1426l.apk";
        }

        //[WebMethod]
        //public bool SubmitPoints(List<TrackPoint> points)
        //{
        //    VehicleManagementDataContext context = VehicleManagementDataContextManager.Instance;
        //    Table<TrackPoint> trackPoints = context.GetTable<TrackPoint>();
        //    foreach (TrackPoint point in points)
        //    {
        //        point.TrackPointID = Guid.NewGuid();
        //    }
        //    try
        //    {
        //        trackPoints.InsertAllOnSubmit(points);
        //        context.SubmitChanges();
        //        return true;
        //    }
        //    catch (Exception)
        //    {
        //        return false;
        //    }
        //}
        /// <summary>
        /// 提交点记录
        /// </summary>
        /// <param name="jsonPointList"></param>
        /// <returns></returns>
        [WebMethod]
        public bool SubmitPoints(string jsonPointList)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            List<TrackPoint> points = jss.Deserialize<List<TrackPoint>>(jsonPointList);
            VehicleManagementDataContext context = VehicleManagementDataContextManager.Instance;
            Table<TrackPoint> trackPoints = context.GetTable<TrackPoint>();
            foreach (TrackPoint point in points)
            {
                point.TrackPointID = Guid.NewGuid();
            }
            try
            {
                trackPoints.InsertAllOnSubmit(points);
                context.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 更新路线跟踪记录
        /// </summary>
        /// <param name="id"></param>
        /// <param name="timeCost"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        [WebMethod]
        public bool UpdateTrack(int id,string timeCost,int state)
        {
       //     Table<Track> tracks = VehicleManagementDataContextManager.Instance.GetTable<Track>();
        //    Track track = tracks.Single(t => t.TrackID == id);
            double timecost = Double.Parse(timeCost);

            VehicleManagementDataContext context = VehicleManagementDataContextManager.Instance;
            Track track = context.GetTable<Track>().Single(t => t.TrackID == id);
            track.TimeCost = timecost;
            track.State = state; 
            try
            {
                context.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }            
          
        }
        /// <summary>
        /// 获取路线历史记录
        /// </summary>
        /// <param name="beginning"></param>
        /// <param name="destination"></param>
        /// <returns></returns>
        [WebMethod]
        public string GetTrackHistory(string beginning, string destination)
        {
            Table<Track> tracks = VehicleManagementDataContextManager.Instance.GetTable<Track>();
            var result = from track in tracks
                         where track.Beginning == beginning
                            && track.Destination == destination
                            && track.State == 1
                         select track;
            JavaScriptSerializer jss = new JavaScriptSerializer();
            return jss.Serialize(result);
        }
        /// <summary>
        /// 根据TrackID获取跟踪点
        /// </summary>
        /// <param name="trackID"></param>
        /// <returns></returns>
        [WebMethod]
        public string GetTrackPointsByTrackID(int trackID)
        {
            Table<TrackPoint> trackPoints = VehicleManagementDataContextManager.Instance.GetTable<TrackPoint>();
            var result = from trackPoint in trackPoints
                         where trackPoint.TrackID == trackID
                         orderby trackPoint.Time
                         select trackPoint;
            JavaScriptSerializer jss = new JavaScriptSerializer();
            return jss.Serialize(result);
        }
    }
}
